//
//  ZixunCell.swift
//  YunshuApp
//
//  Created by 周文杰 on 2017/5/21.
//  Copyright © 2017年 zwj. All rights reserved.
//

import UIKit

class ZixunCell: UITableViewCell {

    let dateLabel = UILabel()
    let loveImg = UIImageView()
    let loveCount = UILabel()
    
    let titleLabel = UILabel()
    let imgView = UIImageView()
    let line = UIView()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.titleLabel.font = UIFont.systemFont(ofSize: 14)
        self.titleLabel.textColor = UIColor.black
        self.titleLabel.numberOfLines = 2
        self.contentView.addSubview(self.titleLabel)

        self.dateLabel.font = UIFont.systemFont(ofSize: 11)
        self.dateLabel.textColor = Const.COLOR_GRAY
        self.contentView.addSubview(self.dateLabel)
        
        self.loveCount.font = UIFont.systemFont(ofSize: 11)
        self.loveCount.textColor = Const.COLOR_GRAY
        self.contentView.addSubview(self.loveCount)
        
        self.loveImg.image = UIImage.init(named: "favourate1")
        self.contentView.addSubview(self.loveImg)

        self.imgView.frame = CGRect.init(x: 10, y: 10, width: 40, height: 40)
        self.imgView.backgroundColor = Const.WTM_WHITE_SMOKE
        self.imgView.layer.cornerRadius = 4
        self.contentView.addSubview(self.imgView)

        line.frame = CGRect.init(x: 0, y: 0, width: Const.SCREEN_WIDTH, height: Const.ONE_PIXEL)
        line.backgroundColor = Const.WTM_GRAY
        self.contentView.addSubview(line)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func setTitle(_ text: String) {
        self.titleLabel.text = text
        let width = Const.SCREEN_WIDTH - 70 
        
        var height = UIUtility.calcStringHeight(text, font: self.titleLabel.font, refWidth: width)
        if height > 40 {
            height = 40
        }
        self.titleLabel.frame = CGRect.init(x: 60, y: 10, width: width, height: height)
        self.dateLabel.frame = CGRect.init(x: 60, y: 10 + height + 10, width: 100, height: 13)
        var center = self.imgView.center
        center.y = (height + 45) / 2
        self.imgView.center = center
        
        self.loveImg.frame = CGRect.init(x: Const.SCREEN_WIDTH - 50, y: 10 + height + 10, width: 15, height: 15)
        
        self.loveCount.frame = CGRect.init(x: Const.SCREEN_WIDTH - 30, y: 10 + height + 10, width: 15, height: 15)
        
        line.frame = CGRect.init(x: 0, y: 10 + height + 10 + 25 - Const.ONE_PIXEL, width: Const.SCREEN_WIDTH, height: Const.ONE_PIXEL)
        
    }
    
    static func heightWithText( _ text: String) -> CGFloat {
        let width:CGFloat = Const.SCREEN_WIDTH - 70
        
        var height = UIUtility.calcStringHeight(text, font: UIFont.systemFont(ofSize: 14), refWidth: width)
        if height > 40 {
            height = 40
        }
        return 10 + height + 10 + 25
    }
}
